<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<!--查询部门表单-->
<form class="layui-form" action="" lay-filter="departmentForm">
    <div class="layui-form-item" style="margin-top:10px">
        <span id="gll" class="layui-btn" lay-submit="" lay-filter="pageSubmit">产品管理</span>
        <span id="add" class="layui-btn" lay-submit="" lay-filter="pageSubmit">增加商品</span>
        <span id="dee" class="layui-btn" lay-submit="" lay-filter="pageSubmit">下架商品</span>
    </div>
</form>

<script type="text/html" id="barDemo">
    <a id="de1" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="de1">查询</a>
</script>
<script type="text/html" id="barDemo10">
    <a id="de10" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="de10">修改信息</a>
</script>
<script type="text/html" id="barDemo11">
    <a id="de11" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="de11">更新价格</a>
</script>
<script type="text/html" id="barDemo12">
    <a id="de12" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="de12">下架</a>
</script>

<!--部门表单-->
<table class="layui-hide" id="test" lay-filter="test"></table>

<script src="../../layui/layui.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table', 'form', 'layer'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        function tableReload() {
            var data = form.val('departmentForm');
            table.reload('departmentTable', {
                where: data
                , page: {
                    curr: 1
                }
            })
        }

        $("#formsubmit").click(function () {
            tableReload();
        })

        table.render({
            id: 'departmentTable'
            , elem: '#test'
            , url: '/firstWeb/sort_DP'
            , title: '产品管理'
            , where: {}
            , cols: [[
                {field: 'p_id', title: '序列号'}
                , {field: 'p_name', title: '产品名称'}
                , {field: 's_name', title: '类别名称'}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
            ]]
            , page: true // 默认一页十条
            , limit: 12
            , limits: [1, 5, 10, 20]
        });

        $("#gll").click(function () {
            table.render({
                id: 'departmentTable'
                , elem: '#test'
                , url: '/firstWeb/sort_DP'
                , title: '产品管理'
                , where: {}
                , cols: [[
                    {field: 'p_id', title: '序列号'}
                    , {field: 'p_name', title: '产品名称', templet: '#pNameTpl'}
                    , {field: 's_name', title: '类别名称'}
                ]]
                , page: true // 默认一页十条
                , limit: 12
                , limits: [1, 5, 10, 20]
            });
        })

        $("#add").click(function () {
            layer.open({
                type: 2 //此处以iframe举例
                , title: '新增页面'
                , area: ['360px', '400px']
                , shade: 0
                , maxmin: true
                , content: 'addProduce.html'
                , btn: ['提交', '关闭']
                , yes: function (index) {
                    var data = window['layui-layer-iframe' + index].getValue();
                    if (!data.s_name) {
                        layer.msg("类别名称不能为空");
                        return false;
                    }

                    //发送新增请求 get/post √
                    axios.post('/firstWeb/sort_insert', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "0") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("新增成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                    layer.close(index);
                }
                , btn2: function (index) {//5
                    layer.close(index);
                }
            });
        })

        $("#dee").click(function () {
            table.render({
                id: 'sort_id'
                , elem: '#test'
                , method: 'get'
                , url: '/firstWeb/sort_out'
                , title: '水果类别管理'
                , where: {}
                , cols: [[
                    {field: 's_id', title: '编号'}
                    , {field: 's_name', title: '类别名称'}
                    , {field: 's_remark', title: '类别备注'}
                    , {field: 's_is', title: '产品是否销毁'}
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo2'}
                ]]
                , page: true // 默认一页十条
                , limit: 5
                , limits: [1, 5, 10, 20]
            });
        })

        //头工具栏事件
        table.on(function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
        });
        //监听行工具事件
        /**
         * 当有两个点击事件时，'tool(test)'同名
         * table.on：监听器只能存在一个（只有一个会生效）
         * 所以应该写在里面，用判断条件来区分
         */
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'de1') {
                table.render({
                    id: 'departmentTable'
                    , elem: '#test'
                    , url: '/firstWeb/product_name'
                    , title: '产品管理'
                    , where: {}
                    , cols: [[
                        {field: 'p_id', title: '产品编号'}
                        , {field: 'p_name', title: '产品名称'}
                        , {field: 'p_area', title: '产地'}
                        , {field: 'p_ml', title: '产品的规格'}
                        , {fixed: 'right', title: '修改信息', toolbar: '#barDemo10'}
                        , {fixed: 'right', title: '更新价格', toolbar: '#barDemo11'}
                        , {fixed: 'right', title: '下架', toolbar: '#barDemo12'}
                    ]]
                    , page: true // 默认一页十条
                    , limit: 12
                    , limits: [1, 5, 10, 20]
                });
            }
        });
    });
</script>

</body>
</html>